#include <cstring>
#include <iostream>

using namespace std;

void buffer_write_binary(char**p, char* binary, size_t length) {
	memcpy(*p, binary, length);
	*p += length; // 形参用 char **p ，此处才可以改变*p的值
}

void buffer_write_bin(char*p, char* binary, int length) {
	memcpy(p, binary, length);
	p += length;
}

void test(){
	char oem[32] = "oem";
	char device_type[32] = "device_type";
	char vin[32] = "vin";
	char version[32] = "version";

    char* p = new char[250];
    memset(p, 0, 250);

    size_t oemlen = strlen(oem);

    char* pp = p;  // why?  because when deleting, the pointer should be at original place
    
    // buffer_write_binary(&pp, oem, oemlen);
    // buffer_write_binary(&pp, device_type, strlen(device_type));
    // buffer_write_binary(&pp, vin, strlen(vin));
    // buffer_write_binary(&pp, version, strlen(version));

    buffer_write_bin(pp, oem, oemlen);
    buffer_write_bin(pp, device_type, strlen(device_type));
    buffer_write_bin(pp, vin, strlen(vin));
    buffer_write_bin(pp, version, strlen(version));

    printf("%s\n", oem);
    printf("%s\n", device_type);
    printf("%s\n", p);

    cout << "strlen(p) = " << strlen(p) << endl;

    if (p!=nullptr)
    {
        delete[] p;
        p = nullptr;
    }
}


int main()
{
    test();
    return 0;
}